package com.itheima.reggie.controller;

import ch.qos.logback.classic.turbo.MarkerFilter;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: hamo
 * @Create by: 渭北彭于晏
 * @Date: 2021/11/1 15:20
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService service;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R save(@RequestBody Category category){
        log.info("category:{}",category);
        service.save(category);
        return R.success("新增分类成功");
    }

    @GetMapping("/page")
    public R page(Integer page,Integer pageSize){
        IPage<Category> pag = new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getSort);

        IPage<Category> result = service.page(pag, wrapper);
        return R.success(result);
    }

    @DeleteMapping
    public R Delete(Long id){
        //根据id获取管理对象
        Category category = service.getById(id);
        //获取type值,或1或2
        Integer type = category.getType();

        //若type== 1 ,则为菜品分类
        if (type == 1) {
            LambdaQueryWrapper<Dish> wreapper = new LambdaQueryWrapper<>();
            wreapper.eq(Dish::getCategoryId, id);
            //查询该菜品分类下有几道菜
            int result = dishService.count(wreapper);
            //有菜不可删
            if (result > 1) {
               return R.error("该菜品分类下有菜,不能删");
            }
        }

        //若type== 2 ,则为套餐分类
        if (type == 2){
            LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Setmeal::getCategoryId,id);
            int count = setmealService.count(wrapper);
            if (count > 0){
               return R.error("该套餐分类下有套餐,不能删");
            }
        }

        //无菜无套餐可删
        service.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 分类列表
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R listDish(Category category){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType() != null,Category::getType,category.getType());
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = service.list(wrapper);
        return R.success(list);
    }
}
